package com.wwj.study.rmi.rpc.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//注册
public class RpcServer {

//    private static ExecutorService executorService= Executors.newFixedThreadPool(2);
    private static ExecutorService executorService= Executors.newCachedThreadPool();

    //服务注册
    public static void register(Object o,int port){
        //客户端请求后，需要使用这个o对象来处理方法
        ServerSocket serverSocket=null;
        try {
            //建立一个服务器监听对象
            serverSocket=new ServerSocket(port);
            //循环监听
            while (true){
                Socket socket=serverSocket.accept();
                executorService.execute(new ProcessHandler(socket,o));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
